<script setup>
    import blogTypeApi from "@/api/blogType.js";
    import {ref, reactive} from "vue";
    import {
        Delete,
        Edit,
    } from '@element-plus/icons-vue'
    import {ElMessage, ElMessageBox} from "element-plus";
    
    //表格数据
    const list = ref([])
    const total = ref(0)
    //分页信息和搜索条件
    const blogTypeQuery = reactive({
        name: '',
        beginCreateTime: '',
        endCreateTime: '',
        page: 1,
        limit: 10
    })
    
    const loadData = () => {
        blogTypeApi.list(blogTypeQuery).then(result => {
            list.value = result.data.list
            total.value = result.data.total
        })
    }
    
    loadData()
    
    const onSearch = () => {
        blogTypeQuery.page = 1;
        loadData();
    }
    
    //根据Id删除
    const deleteById = (id) => {
        ElMessageBox.confirm(
            '确认要删除吗？',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
                lockScroll: false //防止抖动
            }
        ).then(() => {
            blogTypeApi.deleteById(id).then(result => {
                if (result.code === 0) {
                    ElMessage({message: result.msg, type: 'success',})
                } else {
                    ElMessage.error(result.msg)
                }
                loadData()
            })
        })
    }
    
    //批量删除
    const ids = ref([])
    const handleSelectionChange = (rows) => {
        //console.log('多选',rows);
        ids.value = rows.map(row => row.id);
        console.log(ids.value);
    }
    const deleteAll = () => {
        ElMessageBox.confirm(
            '确认要删除吗？',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
                lockScroll: false //防止抖动
            }
        ).then(() => {
            blogTypeApi.deleteAll(ids.value).then(result => {
                if (result.code === 0) {
                    ElMessage({message: result.msg, type: 'success',})
                } else {
                    ElMessage.error(result.msg)
                }
                loadData()
            })
        })
    }
    
    //添加和修改
    const dialogFormVisible = ref(false)
    const blogType = ref({})
    const title = ref()
    
    const showAddDialog = () => {
        dialogFormVisible.value = true
        title.value = '添加'
        blogType.value = {}
    }
    const showUpdateDialog = (id) => {
        dialogFormVisible.value = true
        title.value = '编辑'
        blogType.value = {}
        blogTypeApi.selectById(id).then(result => {
            blogType.value = result.data
        })
    }
    
    const addOrUpdate = () => {
        if (blogType.value.id) {
            blogTypeApi.update(blogType.value).then(result => {
                if (result.code === 0) {
                    ElMessage({message: result.msg, type: 'success',})
                    dialogFormVisible.value = false
                } else {
                    ElMessage.error(result.msg)
                }
                loadData()
            })
        } else { //添加
            blogTypeApi.add(blogType.value).then(result => {
                if (result.code === 0) {
                    ElMessage({message: result.msg, type: 'success',})
                    dialogFormVisible.value = false
                } else {
                    ElMessage.error(result.msg)
                }
                loadData()
            })
        }
    }
    
    import {useTokenStore} from "@/store/token.js";
    const tokenStore = useTokenStore();
    const headers = ref({
        //添加token
        Authorization: tokenStore.token
    })
</script>

<template>
    <el-card class="">
        <template #header>
            <div class="header">
                <el-button type="primary" @click="showAddDialog">添加</el-button>
                <el-button type="danger" @click="deleteAll">批量删除</el-button>
            </div>
        </template>

        <el-form :inline="true">
            <el-form-item label="类型名称">
                <el-input v-model="blogTypeQuery.name" placeholder="请输入类型名称" clearable />
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                    v-model="blogTypeQuery.beginCreateTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="开始时间"
                />
                <el-date-picker
                    v-model="blogTypeQuery.endCreateTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="结束时间"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="list" style="width: 100%" ref="multipleTableRef" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column fixed prop="id" label="ID" width="150" />
            <el-table-column prop="name" label="类型名称"/>
            <el-table-column prop="createTime" label="创建时间"/>
            <el-table-column prop="updateTime" label="更新时间"/>
            <el-table-column fixed="right" label="Operations" min-width="120">
                <template #default="{ row }">
                    <el-button type="primary" :icon="Edit" circle @click="showUpdateDialog(row.id)"/>
                    <el-button type="danger" :icon="Delete" circle @click="deleteById(row.id)"/>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="blogTypeQuery.page"
            v-model:page-size="blogTypeQuery.limit"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @change="loadData"
            style="margin-top: 20px; justify-content: flex-end"/>

        <!--添加、编辑的弹出框-->
        <el-dialog v-model="dialogFormVisible" :title="title" width="500" :lock-scroll="false">
            <el-form :model="blogType">
                <el-form-item label="类型名称" :label-width="70">
                    <el-input v-model="blogType.name" autocomplete="off" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="addOrUpdate">
                        确认
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </el-card>
</template>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        text-align: center;
    }
</style>